package org.gruposp2p.dnie.client.ui.panel;

import java.util.ArrayList;
import java.util.List;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author jj
 */
public class PublicCentralPanel
        extends Composite implements ValueChangeHandler<String> {
	
    public static String HISTORY_LAST_DOCUMENTS = "#lastDocuments";
    public static String HISTORY_MOST_SIGNED_DOCUMENTS = "#mostSignedDocuments";
    public static String HISTORY_SEARCH_PANEL = "#searchPanel";
    
    private static PublicCentralPanelUiBinder uiBinder = GWT
                    .create(PublicCentralPanelUiBinder.class);
    
    interface PublicCentralPanelUiBinder extends UiBinder<Widget, PublicCentralPanel> { }
    
    interface Style extends CssResource {
        String selected();
        String notSelected();
    }

    @UiField Style style;
	
    @UiField VerticalPanel selectedPanel;
    @UiField HorizontalPanel menuPanel;
    @UiField LastDocumentsPanel lastDocuments;
    @UiField MostSignedDocumentsPanel mostSignedDocuments;
    @UiField SearchPanel searchPanel;  
	
    @UiField Hyperlink lastDocumentsLink;
    @UiField Hyperlink mostSignedDocumentsLink;
    @UiField Hyperlink searchPanelLink;

    List<Hyperlink> hyperlinks = null;

    public PublicCentralPanel() {
        initWidget(uiBinder.createAndBindUi(this));
        hyperlinks = new ArrayList<Hyperlink>();
        hyperlinks.add(lastDocumentsLink);
        hyperlinks.add(mostSignedDocumentsLink);
        hyperlinks.add(searchPanelLink);
        setSelected(HISTORY_LAST_DOCUMENTS);
        History.addValueChangeHandler(this);
    }

    public void setPanel (String historyToken) {
    	if (selectedPanel != null) selectedPanel.clear();
    	selectedPanel.setHeight("400px");
        if (historyToken.equals(HISTORY_LAST_DOCUMENTS)){
            selectedPanel.add(lastDocuments.getInstance());
        } else if (historyToken.equals(HISTORY_MOST_SIGNED_DOCUMENTS)){
            selectedPanel.add(mostSignedDocuments.getInstance());
        } else if (historyToken.equals(HISTORY_SEARCH_PANEL)){
            selectedPanel.add(searchPanel.getInstance());
        }
    }

    @Override
    protected void onLoad() {
    	String historyToken = History.getToken();
    	if (historyToken == null) {
    		History.newItem(HISTORY_LAST_DOCUMENTS);
    	}
    }

    private void setSelected (String historyToken) {
        Hyperlink selectedHyperlink = null;
        if (HISTORY_LAST_DOCUMENTS.equals(historyToken)) {
            selectedHyperlink = lastDocumentsLink;
        } else if (HISTORY_MOST_SIGNED_DOCUMENTS.equals(historyToken)) {
            selectedHyperlink = mostSignedDocumentsLink;
        } else if (HISTORY_SEARCH_PANEL.equals(historyToken)) {
            selectedHyperlink = searchPanelLink;
        }
        if (selectedHyperlink != null) {
            for (Hyperlink hyperlink : hyperlinks) {
                if (hyperlink.equals(selectedHyperlink)) {
                    hyperlink.removeStyleName(style.notSelected());
                    hyperlink.addStyleName(style.selected());
                } else {
                    hyperlink.removeStyleName(style.selected());
                    hyperlink.addStyleName(style.notSelected());
                }
            }
            setPanel(historyToken);
            MainPanel.INSTANCE.setSelectedPanel(this);
        }
    }

    @Override
    public void onValueChange(ValueChangeEvent<String> event) {
        if (MainPanel.INSTANCE.loading) return;
        String historyToken = event.getValue();
        Log.debug("PublicCentralPanel - historyToken: " + historyToken);
        setSelected(historyToken);
    }
    
}